<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="姓名" prop="psyName">
        <el-input
          v-model="queryParams.psyName"
          placeholder="请输入配送员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="psyPhone">
        <el-input
          v-model="queryParams.psyPhone"
          placeholder="请输入配送员手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作类型" prop="psyGzlx">
        <el-select v-model="queryParams.psyGzlx" placeholder="请选择配送员工作类型 0全职专送 1兼职众包" clearable>
          <el-option
            v-for="dict in dict.type.psy_gzlx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工作城市" prop="psyGzcs">
        <el-input
          v-model="queryParams.psyGzcs"
          placeholder="请输入工作城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配送站名字" prop="pszName">
        <el-input
          v-model="pszName"
          placeholder="请输入配送站名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分" prop="psyPingfen">
        <el-input
          v-model="queryParams.psyPingfen"
          placeholder="请输入评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="psyShimingrenzheng">
        <el-select v-model="queryParams.psyShimingrenzheng" placeholder="请选择认证状态，0未认证，1已认证" clearable>
          <el-option
            v-for="dict in dict.type.psy_shimingrenzheng"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker
          v-model="daterangePsyZhuceshijian"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="配送员状态" prop="psyStatus">
        <el-select v-model="queryParams.psyStatus" placeholder="请选择配送员状态 0未开通 1接单中 2休息中 3已禁用 4已离职 5已注销" clearable>
          <el-option
            v-for="dict in dict.type.psy_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:peisongyuan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:peisongyuan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:peisongyuan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:peisongyuan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="peisongyuanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配送员id" align="center" prop="psyId" />
      <el-table-column label="配送员姓名" align="center" prop="psyName" />
      <el-table-column label="配送员手机号" align="center" prop="psyPhone" />
      <el-table-column label="配送员工作类型" align="center" prop="psyGzlx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.psy_gzlx" :value="scope.row.psyGzlx"/>
        </template>
      </el-table-column>
      <el-table-column label="工作城市" align="center" prop="psyGzcs" />
      <el-table-column label="所属配送站" align="center" prop="peisongzhan.pszName" />
      <el-table-column label="配送员完成单" align="center" prop="psyFinishcount" />
      <el-table-column label="评分" align="center" prop="psyPingfen" />
      <el-table-column label="认证状态" align="center" prop="psyShimingrenzheng">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.psy_shimingrenzheng" :value="scope.row.psyShimingrenzheng"/>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="psyZhuceshijian" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.psyZhuceshijian, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="配送员状态" align="center" prop="psyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.psy_status" :value="scope.row.psyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:peisongyuan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:peisongyuan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配送员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="psyName">
          <el-input v-model="form.psyName" placeholder="请输入配送员姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="psyPhone">
          <el-input v-model="form.psyPhone" placeholder="请输入配送员手机号" />
        </el-form-item>
        <el-form-item label="工作类型" prop="psyGzlx">
          <el-select v-model="form.psyGzlx" placeholder="请选择配送员工作类型 0全职专送 1兼职众包">
            <el-option
              v-for="dict in dict.type.psy_gzlx"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作城市" prop="psyGzcs">
          <el-input v-model="form.psyGzcs" placeholder="请输入工作城市" />
        </el-form-item>
<!--        <el-form-item label="配送站id" prop="pszId">-->
<!--          <el-input v-model="form.pszId" placeholder="请输入配送站id" />-->
<!--        </el-form-item>-->
        <el-form-item label="所属配送站" prop="pszId">
          <el-select v-model="form.pszId"
                     placeholder="请选择下拉选择"
                     clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in peisongzhanOptions"
                       :key="index"
                       :label="item.pszName"
                       :value="item.pszId"
                       :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评分" prop="psyPingfen">
          <el-input v-model="form.psyPingfen" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="认证状态" prop="psyShimingrenzheng">
          <el-select v-model="form.psyShimingrenzheng" placeholder="请选择认证状态，0未认证，1已认证">
            <el-option
              v-for="dict in dict.type.psy_shimingrenzheng"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="注册时间" prop="psyZhuceshijian">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.psyZhuceshijian"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择注册时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="配送员状态" prop="psyStatus">
          <el-select v-model="form.psyStatus" placeholder="请选择配送员状态 0未开通 1接单中 2休息中 3已禁用 4已离职 5已注销">
            <el-option
              v-for="dict in dict.type.psy_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPeisongyuan, getPeisongyuan, delPeisongyuan, addPeisongyuan, updatePeisongyuan } from "@/api/system/peisongyuan";
import { listShenhezhuangtai } from '@/api/system/yonghuxinxishenhe'
import {listPeisongzhan} from '@/api/system/peisongzhan'

export default {
  name: "Peisongyuan",
  dicts: ['psy_gzlx', 'psy_status', 'psy_shimingrenzheng'],
  data() {
    return {
      peisongzhanOptions:[],
      pszName:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配送员管理表格数据
      peisongyuanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 配送员状态 0未开通 1接单中 2休息中 3已禁用 4已离职 5已注销时间范围
      daterangePsyZhuceshijian: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        psyName: null,
        psyPhone: null,
        psyGzlx: null,
        psyGzcs: null,
        pszId: null,
        psyPingfen: null,
        psyShimingrenzheng: null,
        psyZhuceshijian: null,
        psyStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询配送员管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePsyZhuceshijian && '' != this.daterangePsyZhuceshijian) {
        this.queryParams.params["beginPsyZhuceshijian"] = this.daterangePsyZhuceshijian[0];
        this.queryParams.params["endPsyZhuceshijian"] = this.daterangePsyZhuceshijian[1];
      }
      if (null != this.pszName && '' != this.pszName) {
        this.queryParams.params["pszName"] = this.pszName;
      }
      listPeisongyuan(this.queryParams).then(response => {
        this.peisongyuanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getpeisongzhanOptions(){
      listPeisongzhan().then(response => {
        this.peisongzhanOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        psyId: null,
        psyName: null,
        psyPhone: null,
        psyGzlx: null,
        psyGzcs: null,
        pszId: null,
        psyPingfen: null,
        psyShimingrenzheng: null,
        psyZhuceshijian: null,
        psyStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePsyZhuceshijian = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.psyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配送员管理";
      this.getpeisongzhanOptions();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const psyId = row.psyId || this.ids
      getPeisongyuan(psyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配送员管理";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.psyId != null) {
            updatePeisongyuan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPeisongyuan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const psyIds = row.psyId || this.ids;
      this.$modal.confirm('是否确认删除配送员管理编号为"' + psyIds + '"的数据项？').then(function() {
        return delPeisongyuan(psyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/peisongyuan/export', {
        ...this.queryParams
      }, `peisongyuan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
